const {BaseService} = require('../../basic.js')
const {
	CandidateModel
} = require('dx-func-entity')
const _ = require('underscore');
const dayjs = require('dayjs');
// const {
// 	buildMenus
// } = require('../util/util.js')

module.exports = class Candidate extends BaseService {
	constructor(ctx) {
		super(ctx);
		this.entity = CandidateModel;		
	}
	company() {
	    //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
		return this.hasOne('human-company', 'company_id', '_id').bindField({
			company_name: 'name',
		});
	}
	delByTid(ids){
		let _ids
		if(!_.isArray(ids)){
			_ids = [ids];
		}else{
			_ids= ids;
		}
		return this.where('talent_id','in',_ids).delete();
	}
	talent(){
		return this.hasOne('human-talent', 'talent_id', '_id').bindField({
			talent_name: 'name',
			has_link:'link',
			mobile:'mobile',
			from:'from',
			picture:'picture'
		});
	}
	
	findByIds(_ids){
		return this.where('_id','IN',_ids).limit(1000).select();
	}
	async findByNames(names=[]){
		const list = await this.where({name:['in',names]}).limit(1000).select();
		if(list.length==1000){
			const lst2 = await this.where({name:['in',names]}).skip(1000).limit(1000).select();
			list.push(...lst2);
		}
		return list;
	}
	async getByProjectId({_id}){
		if(!_id){
			return this.$dx.error(' 项目 ID 不能为空');
		}
		const where = {
			'project_id':_id
		};
		const candidates = await this.$dx.service.talent.where(where).with(['talent']).limit(1000).select();
		return candidates;
	}
	async joinProject({from,uid,talent_id,project_id}){
		const project = await this.$dx.service.project.where({_id:project_id}).field('name').find()
		if(!project){
			return this.$dx.error('项目不存在');
		}
		const candidate = await this.where({talent_id,project_id}).find();
		if(candidate){
			return this.$dx.error('该候选人本项目中已存在',candidate);
		}
		const candidate_id = await this.insert({
			talent_id,
			project_id,
			user_id:uid,
			promot_list:[]
		});
		return this.$dx.success('加入成功',candidate_id);
	}
	async joinCandidate({from,talent_id,project_id,target_id,target_name}){
		const project = await this.$dx.service.project.where({_id:project_id}).field('name,target_ids').find()
		if(!project){
			return this.$dx.error('项目不存在');
		}
		/****************************************/
		if(from=='talent'){
			const candidate = await this.where({talent_id,project_id}).find();
			if(candidate){
				return this.$dx.error('该候选人本项目中已存在',candidate);
			}
			await this.insert({
				talent_id,
				project_id,
				target_name,
				company_id:target_id,
				promot_list:[]
			})
		}
		/*****************加入目标客户*********************/
		let target_ids = project.target_ids||[];
		target_ids = _.uniq([...target_ids,...(Array.isArray(target_id)?target_id:[target_id])]);
		if(!project.target_ids||target_ids.length>project.target_ids.length){
			const result = await this.$dx.service.project.update({
				_id:project_id,
				target_ids
			});
		}else if(from=='company'){
			return this.$dx.error('该企业在本项目中已存在');
		}
		
		/**************************************/
		
		return this.$dx.success('加入成功');
	}
	async addPromot({_id,pname,status,sname,comment,target_name}){
		const user_id = this.$dx.auth.uid;
		const create_date = new Date().getTime();
		if(!status&&!comment&&!target_name)return this.$dx.error('候选人状态未做变动');
		const promot = {
			user_id,
			create_date
		}
		if(status){
			promot.status = status;
		}
		if(comment){
			promot.comment = comment;
		}
		let promot_list = [];
		if(status||comment){
			const candidate = await this.where({_id}).field('name,talent_id,promot_list').find();
			promot_list = [...candidate.promot_list,promot];
			/*更新简历的回访记录*/
			const pdata = {
				content:'【'+pname+'】'+ (comment||'')+(status?('，状态：'+sname):''),
				user_id,
				timestamp:dayjs().add(8, 'hour').format('YYYY-MM-DD HH:mm:ss')
			}
			await this.$dx.service.talent.addPromot({_id:candidate.talent_id,promot:pdata});
		}
		
		const params = {
			_id
		}
		if(target_name){
			params.target_name = target_name;
		}
		if(promot_list&&promot_list.length){
			params.promot_list = promot_list;
		}
		if(!params.target_name&&!params.promot_list){
			return this.$dx.error('候选人状态未做变动')
		}
		const result = await this.update(params);
		return this.$dx.success('更新成功',result);
	}
	async updateMerge({_id,_ids}){
		return this.where({'company_id':['in',_ids]}).update({
			company_id:_id
		});
	}
	async updateTMerge({_id,_ids}){
		return this.where({'talent_id':['in',_ids]}).update({
			talent_id:_id
		});
	}
}
